package com.jas.ui;



import java.util.ArrayList;
import java.util.Scanner;
import java.util.TreeMap;

import com.jas.util.Language;

/**
 * This class handles operations related to the program display
 * 
 * 
 * 
 */

public class JasDisplayHandler {

	ArrayList<String> myInputArray;
	TreeMap<String, Integer> tm;

	/**
	 * These are the possible command types
	 * 
	 * 
	 */
	enum CommandType {
		CHANGE_LANGUAGE,ADD_TASK, DELETE_TASK, CLEAR_ALL_TASKS, SHOW_ALL_TASKS, EXIT, INVALID, SEARCH, SORT
	};



	/**
	 * This constructor initialises JasDisplayHandler object.
	 * 
	 * @param args
	 *            User's input parameter.
	 */
	public JasDisplayHandler() {
		this.tm = new TreeMap<String, Integer>();
		addCommand();
		this.myInputArray = new ArrayList<String>();
		
		

	}



	/**
	 * This method executes any command entered by the user.
	 * 
	 */
	public void executeUserCommand() {

		Scanner sc = new Scanner(System.in);
		String input;
		do {
			System.out.print(Language.getString("PROMPT_COMMAND"));
			input = sc.nextLine();

			// split input text entered by user
			String[] word = input.split(" ");
			// get command entered by user
			String userCommand = word[0];
			// extract the text
			String sentence = getUserText(word);
			// execute the command
			CommandType commandType = determineCommandType(userCommand);
			switch (commandType) {
			case CHANGE_LANGUAGE:
				Language.currentLanguage=sentence;
				Language.loadLanguageFile();
				break;
			case ADD_TASK:
				
				break;
			case SHOW_ALL_TASKS:
				
				break;
			case DELETE_TASK:
				
				break;
			case CLEAR_ALL_TASKS:
				
				break;
			case EXIT:
				System.exit(0);
				break;
			case INVALID:
				System.out.println(Language.getString("ERROR_UNRECOGNISED_COMMAND"));
				break;
			case SEARCH:
				
				break;
			
				
			default:
				// throws an error if command is unrecognized
				System.out.println(Language.getString("ERROR_UNRECOGNISED_COMMAND"));
				
			}

		} while (!input.equalsIgnoreCase("exit"));
		sc.close();
	}

	

	/**
	 * This method defines the commands available to the user.
	 * 
	 * @param commandTypeString
	 *            is the first word of the user command
	 */
	private CommandType determineCommandType(String commandTypeString) {
		CommandType result = null;
		if (commandTypeString == null)
			throw new Error(Language.getString("ERROR_UNRECOGNISED_COMMAND"));
		// convert to lower case
		String commandToLowerCase = commandTypeString.toLowerCase();

		try {
			// Determine action for user's command type
			int assignCommand = tm.get(commandToLowerCase);
			switch (assignCommand) {
			case 1:
				result = CommandType.ADD_TASK;

				break;
			case 2:
				result = CommandType.DELETE_TASK;

				break;
			case 3:
				result = CommandType.CLEAR_ALL_TASKS;

				break;
			case 4:
				result = CommandType.SHOW_ALL_TASKS;

				break;
			case 5:
				result = CommandType.EXIT;

				break;
			case 6:
				result = CommandType.SEARCH;

				break;
			case 7:
				result = CommandType.SORT;

				break;
				
			case 8:
				result = CommandType.CHANGE_LANGUAGE;

				break;

			default:
				result = CommandType.INVALID;
			}
		} catch (Exception e) {
			result = CommandType.INVALID;
		}

		return result;
	}

	/**
	 * This method adds all the commands used in this program.
	 */
	private void addCommand() {
		// Put all command types to the map
		tm.put("add", 1);
		tm.put("delete", 2);
		tm.put("clear", 3);
		tm.put("display", 4);
		tm.put("exit", 5);
		tm.put("search", 6);
		tm.put("sort", 7);
		tm.put("lang", 8);
	}

	/**
	 * This method extracts the text entered by the user before writing to the
	 * text file.
	 * 
	 * @param word
	 *            The text entered by the user split into an array.
	 * @return The newly constructed sentence.
	 */
	private String getUserText(String[] word) {
		String sentence = "";
		// recombine text
		for (int i = 1; i < word.length; i++) {
			sentence = sentence + word[i];
			if (i != word.length - 1)
				sentence = sentence + " ";
		}
		return sentence;
	}

	



}